package com.tqjc.business.interactivewithscan.core.thread.PTZ;

import com.tqjc.business.interactivewithscan.core.thread.NormalThread;
import com.tqjc.business.interactivewithscan.entity.DeviceSocketClient;
import com.tqjc.business.interactivewithscan.service.ScanDeviceDataService;
import com.tqjc.business.interactivewithscan.service.impl.ScanDeviceDataServiceImpl;
import com.tqjc.system.core.util.SpringUtils;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetSocket;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @program: C20005_Xianggang_new
 * @description: 单轨槽车扫描仪
 * @author: Patienyon
 * @create: 2023-11-08 09:58
 **/
//@Service
@Slf4j
public class SingleTrackPZTThread extends NormalThread {
//    private BMaterialMapper materialMapper = SpringUtils.getBean(BMaterialMapper.class);

    private boolean start = false;
    private int index = 0;

    private ScanDeviceDataService scanDeviceDataService = SpringUtils.getBean(ScanDeviceDataServiceImpl.class);


    public SingleTrackPZTThread() {
        //绑定当前的设备信息
        this.device = scanDeviceDataService.getById(200004);
//        this.device = scanDeviceDataService.getById(999999);
        this.start();
    }

    @Override
    public void run() {
        while (true) {
            try {
                checkSocket();
                Thread.sleep(200);
                // 线程逻辑代码
            } catch (Exception e) {
                // 捕获线程异常
                e.printStackTrace();
                // 线程挂掉后，等待一段时间再重新启动
                try {
                    Thread.sleep(1000); // 可以根据需要调整等待时间
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    @Override
    public void heartbeat(DeviceSocketClient socketClient) {
//        log.error("发送心跳:{}",socketClient.getDevice().getDeviceName());
        NetSocket socket = socketClient.getSocket();
        socket.write(Buffer.buffer("HB"));
        socketClient.getVertx().setPeriodic(5000, t -> {
            socket.write(Buffer.buffer("HB"));
        });
    }

    public void scanningStop() {
        data.remove("scanningResult");
        data.remove("ScanningState");
    }
}
